<?php
namespace Admin\Controller;

class ActivityController extends ComController
{
    public function index()
    {
        $p        = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $offset   = $pagesize * ($p - 1);
        $pagesize = 20;#每页数量
        $order    = I('get.order','DESC');
        $m        = M('Activity');

        $field = '';

        if ($order == 'asc') {
            $order = "stime asc";
        } elseif (($order == 'desc')) {
            $order = "stime desc";
        } else {
            $order = "id desc";
        }

        $keyword = I('keyword') ? I('keyword') : '';

        $map = array(
            'status' => 1,
        );

        if (!empty($keyword)) {
            $map['name'] = array('like', "$keyword%");
            $this->assign('keyword', $keyword);
        }

        $list = $m->where($map)->page($p, $pagesize)->order($order)->select();
        $count = $m->where($map)->count();

        $page = new \Think\Page($count, $pagesize);

        $page = $page->show();

        $this->assign('list', $list);
        $this->assign('page', $page);

        return $this->display();
    }

    public function add()
    {
        if (!IS_POST) {
            return $this->display();
        }

        $data = I('post.');

        if (!isset($data['name']) || empty($data['name'])) {
            return $this->error('参数错误！');
        }

        $saveData = array(
            'name'    => trim($data['name']),
            'content' => trim($data['content']),
            'slogan'  => trim($data['slogan']),
            'pic'  => trim($data['activityPic']),
            // 'stime'   => strtotime($data['starttime']),
            // 'etime'   => strtotime($data['endtime']),
            // 'ctime'   => time(),
            'stime'   => $data['starttime'],
            'etime'   => $data['endtime'],
            'ctime'   => getModeltime(),
        );


        $m = M('activity');
        $res = $m->data($saveData)->filter('strip_tags')->add();

        if (!$res) {
            return $this->success('添加失败');
        }

        return $this->success('添加成功');

    }

    public function edit($id = 0)
    {
        $id = intval($id);

        if (empty($id)) {
            $this->error('参数错误！');
        }

        $m = M('activity');

        $info = $m->where("id='$id'")->find();

        $this->assign('info', $info);
        $this->display();
    }

    public function update(){

        if (!IS_POST) {
            return $this->error('请求方式错误');
        }

        $data = I('post.');

        if (!isset($data['id']) || empty($data['id'])) {
            return $this->error('参数错误！');
        }

        $id = intval($data['id']);

        $saveData = array(
            'name'    => trim($data['name']),
            'content' => trim($data['content']),
            'slogan'  => trim($data['slogan']),
            'stime'   => $data['starttime'],
            'etime'   => $data['endtime'],
            'pic'  => trim($data['activityPic']),
            // 'ctime'   => time(),
        );

        $m = M('activity');
        $res = $m->where(['id'=>$id])->data($saveData)->save();

        if (false === $res) {
            return $this->error('修改失败');
        }

        return $this->success('修改成功','index');
    }

    public function del($id){
        if (empty($id)) {
            return $this->error('参数错误！');
        }

        $res = M('activity')->where(['id'=>$id])->data(['status'=>0])->save();

        if (!$res) {
            return $this->error('删除失败');
        }

        return $this->success('删除成功');
    }

    //活动参数人员
    public function users($id){

    }

    public function joins(){
        $p        = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $offset   = $pagesize * ($p - 1);
        $pagesize = 20;
         $order    = I('get.order','DESC');

        if ($order == 'asc') {
            $order = "ctime asc";
        } elseif (($order == 'desc')) {
            $order = "ctime desc";
        } else {
            $order = "id desc";
        }

        $username = I('username') ? I('username') : '';

        $map = array(
            "j.status" => 1,
        );

        if (!empty($username)) {
            $map['name'] = array('like', "$keyword%");
            $this->assign('username', $username);
        }

        $m = M('activity_joins');

        $list = $m->alias("j")
        ->where($map)
        ->join('LEFT JOIN __USERS__ u ON u.user_id=j.uid')
        ->join('LEFT JOIN __ACTIVITY__ a ON a.id=j.act_id')
        ->join('LEFT JOIN __SHOP__ s ON s.id=a.shopid')
        ->field('j.*,a.name actname,s.name shopname')
        ->page($p, $pagesize)->order($order)->select();

        $count = $m->alias('j')->where($map)
        ->join('LEFT JOIN __USERS__ u ON u.user_id=j.uid')
        ->join('LEFT JOIN __ACTIVITY__ a ON a.id=j.act_id')
        ->join('LEFT JOIN __SHOP__ s ON s.id=a.shopid')
        ->count();

        $page = new \Think\Page($count, $pagesize);

        $page = $page->show();

        $this->assign('list', $list);
        $this->assign('page', $page);

        return $this->display();
    }
}
